//
//  StoreMainViewController.m
//  RainbowFM
//
//  Created by jv on 14-12-14.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "StoreMainViewController.h"
#import "ProductDetailsViewController.h"

#import "KK_TabBarView.h"
#import "GoodsCollectionViewCell.h"
#import <SwipeView/SwipeView.h>
#import "GoodsCatalogModel.h"
#import "CLBannerView.h"
#import <UIImageView+AFNetworking.h>
#import "ADBannerView.h"
#import "ADModel.h"
#import "ADViewController.h"
#import "MerchantViewController.h"

#define TabBar_Tag(index)      1000+index //目录

@interface StoreMainViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UISearchBarDelegate,SwipeViewDataSource,SwipeViewDelegate,CLBannerViewDelegate>{
    NSInteger orderIndex;
    NSString *searchStr;
    NSMutableArray *adListArray;
}
@property (weak, nonatomic) IBOutlet ADBannerView *bannerView;
@property (nonatomic , weak) IBOutlet SwipeView *swipeView;
@property (nonatomic , weak) IBOutlet UISearchBar *searchBar;
@property (nonatomic , weak) IBOutlet UIView *tabBarBackground;
@property (nonatomic , weak) IBOutlet UICollectionView *goodsCollectionView;

@property (nonatomic , strong) NSMutableArray *tabBarViews;//分类选项
@property (weak, nonatomic) IBOutlet UIButton *priceButton;
@property (weak, nonatomic) IBOutlet UIButton *sellButton;
@property (nonatomic , strong) NSMutableArray *goodsList;
@property (nonatomic , strong) NSMutableArray *ads;
@end

@implementation StoreMainViewController
-(void)awakeFromNib
{
    _goodsList = [NSMutableArray array];
//    _tabBarViews = [NSMutableArray array];
//    _ads = [NSMutableArray array];
    
//    [_ads addObjectsFromArray:@[@"http://h.hiphotos.baidu.com/image/pic/item/80cb39dbb6fd5266876a8a1da918972bd4073671.jpg",
//                               @"http://a.hiphotos.baidu.com/image/pic/item/472309f7905298220a8bcf9cd4ca7bcb0a46d431.jpg",
//                                @"http://e.hiphotos.baidu.com/image/pic/item/64380cd7912397dd55c8d9055a82b2b7d0a2877a.jpg"]];
    
    
    [_goodsList addObjectsFromArray:@[@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2"]];
}

-(void)fetchGoodsListWithID:(NSString *)fId secondID:(NSString *)sId{
    [_goodsList removeAllObjects];
    [[GoodsCatalogModel sharedModel] fetchGoodsListWithFirstCatalogID:fId secondCatalogID:sId key:searchStr sortFlag:orderIndex + 1 currentPage:1 pageSize:100 result:^(NSArray *array, NSString *msg) {
        [_goodsList addObjectsFromArray:array];
        [self.goodsCollectionView reloadData];
    }];
}

-(void)fetchADList{
    [adListArray removeAllObjects];
    [[ADModel sharedModel] fetchADListWithChannel:ADChannel code:@"2" result:^(BOOL success, NSArray *array) {
        if (success) {
            NSMutableArray *imageArray = [NSMutableArray array];
            for (ADListItem *item in array) {
                [imageArray addObject:item.imgSmall];
            }
            [adListArray addObjectsFromArray:array];
            self.bannerView.listArray = [NSArray arrayWithArray:imageArray];
            imageArray = nil;
            self.bannerView.scorllAnimation = YES;
            [self.bannerView show];
        }
        
    }];
}

-(void)adBannerSelectedIndex:(NSInteger)index{

    ADListItem *adItem = adListArray[index];
    if ([adItem.type isEqualToString:@"url"]) {
        ADViewController *adView = [self.storyboard instantiateViewControllerWithIdentifier:@"ADViewController"];
        adView.item = adItem;
        [self.navigationController pushViewController:adView animated:YES];
        
    }
    
    else if([adItem.type isEqualToString:@"product"]){
        MerchantViewController *merchantView = [self.storyboard instantiateViewControllerWithIdentifier:@"MerchantViewController"];
        merchantView.merchantid = adItem.relationId;
        merchantView.adItem = adItem;
        [self.navigationController pushViewController:merchantView animated:YES];
    }
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    adListArray = [NSMutableArray array];
    self.navigationItem.leftBarButtonItem = nil;
    _tabBarBackground.layer.masksToBounds = NO;
    orderIndex = 0;
    searchStr = @"";
    
    CLBannerView *bannerView_ =[[[NSBundle mainBundle] loadNibNamed:@"CLBannerView" owner:nil options:nil] lastObject];
    bannerView_.delegate = self;
    [_tabBarBackground addSubview:bannerView_];
    [self fetchGoodsListWithID:@"" secondID:@""];
    [self fetchADList];
    
    
}


- (IBAction)orderByAction:(UIButton *)sender {
    
    if ((sender == _priceButton && _priceButton.selected) || (sender == _sellButton && _sellButton.selected)) {
        return;
    }
    
    if (sender != _priceButton && _priceButton.selected) {
        _priceButton.selected = NO;
        sender.selected = YES;
    }else if(sender != _sellButton && _sellButton.selected){
        _sellButton.selected = NO;
        sender.selected = YES;
    }
    
    orderIndex = sender.tag;
    [self fetchGoodsListWithID:@"" secondID:@""];
}


-(void)bannerSelectedFirstID:(NSString *)firstId sencondID:(NSString *)sencondId{
    [self fetchGoodsListWithID:firstId secondID:sencondId];
}



#pragma mark - action
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)sectio{
    return [_goodsList count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GoodsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GoodsCollectionViewCell" forIndexPath:indexPath];
    GoodsInfoItem *item = _goodsList[indexPath.row];
    cell.nameLabel.text = item.PRODUCT_NAME;
    [cell.goodsImageview sd_setImageWithURL:[NSURL URLWithString:item.PRODUCT_IMG_URL] placeholderImage:nil];
    cell.priceLabel.text = [NSString stringWithFormat:@"%@",_RMB(item.PRE_PRICE)];
    cell.marketLabel.text = [NSString stringWithFormat:@"销量:%d",[item.SALE_NUM intValue]];

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark -SwipeViewDataSource
- (NSInteger)numberOfItemsInSwipeView:(SwipeView *)swipeView
{
    return [_ads count];
}

- (UIView *)swipeView:(SwipeView *)swipeView viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    RFMImageView *showImageView = (RFMImageView *)view;
    if (!showImageView) {
        showImageView = [[RFMImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_swipeView.bounds), CGRectGetHeight(_swipeView.bounds))];
        
        view = showImageView;
    }
    
    [showImageView.imageView sd_setImageWithURL:_ads[index] placeholderImage:nil];
    
    return view;
}

#pragma mark - SwipeViewDelegate
- (void)swipeViewCurrentItemIndexDidChange:(SwipeView *)swipeView
{
    
}

- (void)swipeView:(SwipeView *)swipeView didSelectItemAtIndex:(NSInteger)index
{
    
}

#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    searchStr = searchBar.text;
    [searchBar resignFirstResponder];
    [self fetchGoodsListWithID:@"" secondID:@""];
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"goodsSegue"]) {
        NSIndexPath *indexPath = [_goodsCollectionView indexPathForCell:sender];
        
        GoodsInfoItem *item = _goodsList[indexPath.row];
        
        ProductDetailsViewController *productDetailsViewController = [segue destinationViewController];
        productDetailsViewController.productid = item.PRODUCT_ID;
    }
}


@end
